<?php

namespace app\admin\controller;

use think\Controller;
use think\Db;

class Pops extends Controller
{
    //页面分页
    public function page($page ,$size,$success,$message){
        $return = [
            'data' => '',
            'success' => $success,
            'messages' => $message,
            'page' =>$page,
            'size' => $size
        ];
        $data2 = model('Pops')->order('update_time desc')->paginate($size, false, [
            'page' => $page,
            'var_page' => 'page'
        ]);
        $data = $data2->toArray();

        if ($page>$data['last_page']) {
            $data = model('Pops')->order('update_time desc')->paginate($size, false, [
                'page' => $data['last_page'],
                'var_page' => 'page'
            ]);
            $data = $data->toArray();
            $return['page'] = $data['last_page'];
        }
        $return['data'] = $data['data'];
        $return['total'] = $data['total'];

        $return['page'] = $data['current_page'];
        return $return;
    }

    //添加弹窗
    public function creatPops(){
           $page = input('post.page');
           $size = input('post.size');

            $data = [
                'title' => input('post.title'),
                'maxLoadNum' => input('post.maxLoadNum'),
                'pageview' => 0,
                'status' => 1,
                'author' => session('username'),
                'type' => 1,
                'href' => input('post.href'),
                'photo_link' => input('post.img')

            ];
            $resault = model('Pops')->creatPops($data);
            if ($resault==1){
               $return = $this->page($page,$size,true,"添加成功");
            }else{
                $return = $this->page($page,$size,false,$resault);
            }

        return json($return);

    }
    //删除弹窗
    public function deletePops(){
        $page = input('delete.page');
        $size = input('delete.size');


        $id = input('delete.id');
            $basesInfo = model('Pops')->where('id',$id)->find();
            if (!$basesInfo){
                $return = $this->page($page,$size,false,'查无此弹窗信息');
            }else{
                $result = model('Pops')->where('id',$id)->delete();
                if ($result){
                    $return = $this->page($page,$size,true,"添加成功");
                }else{
                    $return = $this->page($page,$size,false,'失败删除');
                }
            }

        return json($return);

    }
    //分类弹窗
    public function listePops(){
        $page = input('get.page');
        $size = input('get.size');
        $return = $this->page($page,$size,true,"查询成功");

        return json($return);
    }
    //修改弹窗
    public function updatePops(){
        $page = input('put.page');
        $size = input('put.size');

            $data = [
                'title' => input('put.title'),
                'maxLoadNum' => input('put.maxLoadNum'),
                 'href' => input('put.href'),
                'photo_link' => input('put.img')
                 ];
            $id = input('put.id');
            $PopsInfo =model('Pops')->where('id',$id)->find();
            if ($PopsInfo){
                $result = model('Pops')->updatePops($id,$data);
                if ($result==1){
                    $return = $this->page($page,$size,true,"更新成功");
                }else{
                    $return = $this->page($page,$size,false,$result);
                }
            }

        return json($return);

    }
    //更改弹窗模式
    public function  popMode(){
        $return = [
             'success' => false,
            'messages' => '更新失败',

        ];
        $status = input('put.status');

        $result = Db::table('popmode')->where('id',1)->update(['status'=>$status]);
        if ($result){
            $return['success'] =true;
            $return['messages'] = '更改成功';

        }
        return json($return);
    }
    //弹窗模式
    public function  getPopMode(){
        $return = [
            'data'=>'',
            'success' => false,
            'messages' => '查询失败',

        ];

        $result = Db::table('popmode')->where('id',1)->find();
        if ($result){
            $return['data'] = $result;
            $return['success'] =true;
            $return['messages'] = '查询成功';

        }
        return json($return);
    }

    public function publish(){
        $size = input('put.size');
        $page = input('put.page');

        $id = input('put.id');
        $AdvertsInfo =model('Pops')->where('id',$id)->find();
        if ($AdvertsInfo){
            $result = model('Pops')->where('id',$id)->update(['status'=>input('put.status')]);
            if ($result==1){
                $return = $this->page($page,$size,true,"更新成功");

            }else {
                $return = $this->page($page,$size,false,"更改失败");

            }
        }

        return json($return);

    }
}
